﻿@model IEnumerable<ArticleWeb.Models.ArticleCategory>

@{
    ViewBag.Title = "文章分类";
    var rootList = ViewBag.RootList as List<IDName>;
    var allList = ViewBag.AllList as List<IDNameNode>;
}

<p>
    @Html.ActionLink("添加分类", "Create", null, new { @class = "layui-btn layui-btn-primary" })
</p>
<br />
<link href="~/content/jstree/themes/default/style.min.css" rel="stylesheet" />
<ul id="tree"></ul>
<table class="table layui-table">
    <thead>
        <tr>
            <th>ID</th>
            <th>
                名称
            </th>
            <th>
                上级
            </th>
            <th>操作</th>
        </tr>
    </thead>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ParentID)
            </td>
            <td>
                @Html.ActionLink("编辑", "Edit", new { id = item.ID })
            </td>
        </tr>
    }
</table>
@using Webdiyer.WebControls.Mvc;
@Html.Pager(new PagedList<ArticleWeb.Models.ArticleCategory>(Model, Request.GetPageIndex(), Request.GetPageSize(), ViewBag.Total), new PagerOptions
{
    PageIndexParameterName = "page",
    CurrentPagerItemWrapperFormatString = "<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>{0}</em></span>",
}, new { @class = "layui-box layui-laypage layui-laypage-default" })

@section Scripts{
    <script src="~/content/jstree/jstree.min.js"></script>
    <script>
        $.post('/articlecategories/GetJsTreeItems', function (d) {
            $('#tree').on('move_node.jstree', function (e, data) {
                $.post('/articlecategories/UpdateTreeItem', { id: data.node.id, parent: data.parent, position: data.position }, function () {
                    //location.reload();
                });
            }).on('changed.jstree', function (e, data) {
                if (data.action == 'select_node') {
                    layer.confirm('请选择操作', {
                        btn: ['添加子类', '编辑名称'] //按钮
                    }, function () {
                        location.href = '/articlecategories/create?parentId=' + data.node.id;
                    }, function () {
                        location.href = '/articlecategories/edit?id=' + data.node.id;
                    });
                }
            }).on("loaded.jstree", function (event, data) {
                data.instance.close_all();
            }).on("ready.jstree", function (event, data) {
                data.instance.close_all();
            }).jstree({
                'core': {
                    "check_callback": true,
                    'data': d.data
                },
                'contextmenu': function (e, node) {
                    debugger;
                    var temp = {
                        "view": {
                            "label": "详细",
                            "icon": " file",
                            "separator_after": true,
                            "action": function (data) {
                                $("#treeLayout").layout('expand', 'south');
                                var inst = $.jstree.reference(data.reference),
                                    obj = inst.get_node(data.reference);
                            }
                        },
                        "showPhoneBook": {
                            "label": "录音",
                            "action": function (data) {
                                var inst = $.jstree.reference(data.reference),
                                    obj = inst.get_node(data.reference);
                                showRecordWin(mockEvent(obj.id, obj.text));
                            }
                        }
                    };
                    return menu;
                },
                "plugins": ["dnd", "changed", 'contextmenu']
            });
        });
    </script>
}
